#ifndef DBINTERFACEAIS_H
#define DBINTERFACEAIS_H

#include <QObject>
#include <QMap>
#include <QMultiMap>
#include <QSqlDatabase>
#include <QDateTime>
#include <QMutex>

#define AUTHOR "WuLin"
#define SQLS_OF_SINGLE_TRANSACTION_Grid1Degree 40 //一个事务中多少条SQL语句
#define SQLS_OF_SINGLE_TRANSACTION_SmallGrid 1440 //一个事务中多少条SQL语句
#define ROWS_OF_SINGLE_SQL 1620 //一条语句导入多少行数据

struct StructCleanedAisDynamic
{
    //AISDynamicLog表格中，使用的都是大整数
    quint32 recordDateTime;
    qint32 MMSI;
 //   qint64  DRRECORDTYPE;
//    qint64  DRCOMMTYPE;
//    qint64 RCVTIME_int64;
    qint32  longitude;
    qint32  latitude;
    quint16  direction;
    quint16  heading;
    quint16 speed;
    quint8 status;
    qint16  ROT_Sensor;
    qint16  AIS_Source;
};

struct Struct_L1_Ship_History_Position
{
    quint32 Ship_ID;
    qint8 Belief_Ship_ID;

    quint32   Record_GPS_Time;

    qint32  Longitude;
    qint32  Latitude;
    quint16  Direction;
    quint16  Heading;
    quint16 Speed;
    quint8 Status;
    qint16  ROT_Sensor;
    qint16 AIS_Source;
};

struct Struct_L1_Ship_Last_Position
{
    qint64  Ship_ID;
    quint32   Record_Datetime;
    qint32  MMSI;
    qint8   Confidence_MMSI;
    qint16 Ship_Count_With_Same_MMSI;

    qint32  Longitude;
    qint32  Latitude;
    qint16  Direction;
    qint16  Heading;
    qint16 Speed;
    qint16 Status;
    qint32  ROT_Sensor;
    qint16 AIS_Source;
    QDateTime  Upd_Time;
};

struct StructAisStatic
{
    QString MMSI;
    qint64  shipID; //无数据时，填写为-1
    QString ShipName;
    QString CallSign;
    QString CargoType;
    QString ShipType;
    QString Destination;
    QString ETA;
    qint64 Length;
    qint64 Breadth;
    qint64 ClassType;
    QString IMONumber;
    qint64 Draft;
    QString Country;
    qint64 RCVTIME_int64;
    qint64 FIXINGDEVICE;
    QString OperType;
};

struct Struct_L1_Ship_History_Static
{
    qint64 ID;
    qint8 Confidence_Record;

    qint64  ShipID; //无数据时，填写为-1

    quint32 Start_Datetime;
    quint32 End_Datetime;

    qint8 AIS_Familiarity;
    qint8 AIS_Compliance;
    //qint8 Confidence_Lost;

    qint32 MMSI;
    qint8 Confidence_MMSI;
    QString Ship_Name;
    qint8 Confidence_Ship_Name;
    QString Call_Sign;
    qint8 Confidence_Call_Sign;
    qint32 IMO_Number;
    qint8 Confidence_IMO_Number;
    qint16 Ship_Type;
    qint8 Confidence_Ship_Type;
    QString Estimated_Ship_Type;
    qint8 Confidence_Estimated_Ship_Type;
    qint16 Ship_Length;
    qint16 Ship_Breadth;
    qint8 Confidence_Ship_Length;
    qint8 Confidence_Ship_Breadth;
    qint32 Country_Number;
    qint8 Confidence_Country_Number;
    QString Destination;
    qint8 Confidence_Destination;
    QString ETA;
    qint8 Confidence_ETA;
    qint16 Draft;
    qint8 Confidence_Draft;
    qint8   Fixing_Device;
    QString Udt_By;
    QDateTime   Udt_Time;
};

struct Struct_Ship_Static
{
    qint32 MMSI;
    QString Ship_Name;
    QString Call_Sign;
    qint32 IMO_Number;
    qint16 Ship_Type;
    qint16 Ship_Length;
    qint16 Ship_Breadth;
    QString Destination;
    QString ETA;
    qint16 Draft;
};


struct Struct_L1_World_Grids_History
{
    qint32 Upper_Left_Longitude;
    qint32 Upper_Left_Latitude;
    qint32 Lower_Right_Longitude;
    qint32 Lower_Right_Latitudeqint32;
    quint16 utc_Months;
    qint64 crossingTimeSum,DynamicDataCount;
    qint32 day_Vessel_Total_Count,night_Vessel_Total_Count;
};


struct StructDBInfo
{
    QString type;
    QString hostIP;
    QString userName;
    QString pwd;
    QString dbName;
    QString connectionName;
};

class DBInterfaceAIS : public QObject
{
    Q_OBJECT
public:
    explicit DBInterfaceAIS( QMutex *mutex,StructDBInfo structDBInfo,QObject *parent = 0);
    bool connectToDB();
    ~DBInterfaceAIS();

    bool hasFeatureOfTransaction();
    bool startTransaction();
    bool commitTransaction();

    QSqlDatabase *db;

    bool checkConnection(QSqlDatabase * &dbParam);
    QStringList getSourceDBTablePartitions(QString tableName);
    QMutex *mutex;

signals:
    void sigShowInfo(QString str);



public slots:

private:

    StructDBInfo dbInfo;

};

#endif // DBINTERFACEAIS_H
